"""
Case Type   : GUC
Case Name   : 使用gs_guc set方法设置参数enable_codegen为on,观察预期结果
Create At   : 2020/12/2
Owner       : opentestcase005
Description :
        1.查询enable_codegen默认值
        2.gs_guc set设置enable_codegen为on并重启数据库
        3.查询该参数修改后的值
        4.恢复参数默认值
Expect      :
        1.默认值为off
        2.设置成功；重启成功
        3.修改为on成功
        4.参数值恢复成功
History     :modified by opentestcase005 2021/12/8,参数默认值变更，修改用例
             modified by opentestcase005 2023.09.28,参数默认值变更，修改用例
             modified by wangtianjing1 2024.7.2  同步sharestorage目录下优化后的用例
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class QueryPlan(unittest.TestCase):

    def setUp(self):
        self.LOG = Logger()
        self.commonsh = CommonSH('dbuser')
        self.constant = Constant()
        self.LOG.info(
            '------Opengauss_Function_Guc_Queryplan_Case0089 start------')

    def test_enable_codegen(self):
        text = '--step1:查看默认值;expect:默认值为off--'
        self.LOG.info(text)
        sql_cmd = self.commonsh.execut_db_sql('show enable_codegen;')
        self.LOG.info(sql_cmd)
        self.assertEqual('off', sql_cmd.splitlines()[-2].strip(),
                         '执行失败:' + text)

        text = '--step2:gs_guc set设置enable_codegen为on并重启数据库;' \
               'expect:设置成功；重启成功--'
        self.LOG.info(text)
        msg = self.commonsh.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          'enable_codegen =on')
        self.LOG.info(msg)
        self.assertTrue(msg)
        msg = self.commonsh.restart_db_cluster()
        self.LOG.info(msg)
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

        text = '--step3:查询该参数修改后的值;expect:修改为off成功--'
        self.LOG.info(text)
        sql_cmd = self.commonsh.execut_db_sql('show enable_codegen;')
        self.LOG.info(sql_cmd)
        self.assertEqual('on', sql_cmd.splitlines()[-2].strip(),
                         '执行失败:' + text)

    def tearDown(self):
        text = '--step4:恢复默认值;expect:参数值恢复成功--'
        self.LOG.info(text)
        sql_cmd = self.commonsh.execut_db_sql('show enable_codegen;')
        self.LOG.info(sql_cmd)
        if "off" != sql_cmd.split('\n')[-2].strip():
            msg = self.commonsh.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              "enable_codegen=off")
            self.LOG.info(msg)
            msg = self.commonsh.restart_db_cluster()
            self.LOG.info(msg)
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        sql_cmd = self.commonsh.execut_db_sql('show enable_codegen;')
        self.LOG.info(sql_cmd)
        self.assertEqual('off', sql_cmd.splitlines()[-2].strip(),
                         '执行失败:' + text)
        self.LOG.info(
            '-----Opengauss_Function_Guc_Queryplan_Case0089 finish------')
